//
//  XXUIImageIconFont.swift
//  OfferManager
//
//  Created by 谢雄 on 2018/1/7.
//  Copyright © 2018年 OFFER. All rights reserved.
//

import Foundation
import UIKit
import CoreText

extension UIImage {
    
    open class func iconWithInfo(_ iconString: String,fontSize:CGFloat,color:UIColor?) -> UIImage{


        let nscode = iconString
        let rect = nscode.boundingRect(with:CGSize(width: 0.0, height: 0.0) , options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font :  UIFont(name: "IconFont", size: fontSize)!], context: nil)
        let size = rect.size
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        label.font = UIFont(name: "IconFont", size: fontSize)
        label.textAlignment = .center
        label.text = iconString
        if color != nil {
            label.textColor = color!
        }
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        label.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        return image!
    }
}


